//
//  LoadingProgress.m
//  MyPorject
//
//  Created by Yuki on 2021/1/26.
//

#import "ELLoading.h"
#import "NSString+EL.h"
@interface ELLoading ()
/**  MB的Loading动画  **/
@property (nonatomic , strong)MBProgressHUD  *mbWindow_Prohud;
@end

static ELLoading *_instance;

@implementation ELLoading

#pragma mark - --------- 初始化  ---------
+ (instancetype)shareInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL]init];
    });
    return _instance;
}
- (id)copy{
    return [ELLoading shareInstance];
}
- (id)mutableCopy{
    return [ELLoading shareInstance];
}
- (id)copyWithZone:(NSZone *)zone{
    return [ELLoading shareInstance];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    return [self shareInstance];
}

#pragma mark - --------- 加载MB的动画  ---------

- (void)showProgressLoading{
    ///在主线程中加载
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.mbWindow_Prohud) {
            self.mbWindow_Prohud.removeFromSuperViewOnHide = YES;
            [self.mbWindow_Prohud hideAnimated:NO];
            self.mbWindow_Prohud = nil;
        }
        /**  设置样式  **/
        self.mbWindow_Prohud.mode = MBProgressHUDModeIndeterminate;
        self.mbWindow_Prohud.contentColor = UIColor.whiteColor;//指示器颜色
    });
}

/// 显示一个弹框
/// @param title 标题
- (void)showAlertStr:(NSString *)title{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.mbWindow_Prohud) {
            self.mbWindow_Prohud.removeFromSuperViewOnHide = YES;
            [self.mbWindow_Prohud hideAnimated:NO];
            self.mbWindow_Prohud = nil;
        }
        /**  设置样式  **/
        self.mbWindow_Prohud.mode = MBProgressHUDModeText;
        self.mbWindow_Prohud.contentColor = UIColor.whiteColor;//指示器颜色
        self.mbWindow_Prohud.label.text = title;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissProgressLoading];
        });
    });
}

- (void)showProgressLoadingWithStr:(NSString *)title{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.mbWindow_Prohud) {
            self.mbWindow_Prohud.removeFromSuperViewOnHide = YES;
            [self.mbWindow_Prohud hideAnimated:NO];
            self.mbWindow_Prohud = nil;
        }
        /**  设置样式  **/
        self.mbWindow_Prohud.mode = MBProgressHUDModeText;
        self.mbWindow_Prohud.contentColor = UIColor.whiteColor;//指示器颜色
        self.mbWindow_Prohud.label.text = title;
    });
}

///显示到一个父视图上
- (void)showProgressLoadingWithSupview:(UIView *)fatherView{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.mbWindow_Prohud) {
            self.mbWindow_Prohud.removeFromSuperViewOnHide = YES;
            [self.mbWindow_Prohud hideAnimated:YES];
        }
        
        self.mbWindow_Prohud = [MBProgressHUD showHUDAddedTo:fatherView animated:YES];
        self.mbWindow_Prohud.mode = MBProgressHUDModeIndeterminate;
        self.mbWindow_Prohud.contentColor = UIColor.whiteColor;//指示器颜色
        self.mbWindow_Prohud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        self.mbWindow_Prohud.bezelView.backgroundColor = UIColor.blackColor;
        
    });
}

- (void)dismissProgressLoading{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (!self.mbWindow_Prohud) return;
        self.mbWindow_Prohud.removeFromSuperViewOnHide = YES;
        ///结束动画
        [self.mbWindow_Prohud hideAnimated:YES];
    });
}



#pragma mark - --------- lazy  ---------
///MBProgress动画
- (MBProgressHUD *)mbWindow_Prohud{
    if (!_mbWindow_Prohud) {
        _mbWindow_Prohud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].delegate.window animated:YES];
        //结合使用才能修改MB的背景色
        _mbWindow_Prohud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        _mbWindow_Prohud.bezelView.backgroundColor = UIColor.blackColor;
    }
    return _mbWindow_Prohud;
}

@end
